Xbasic

a5_graphQL_execute Function

Syntax

DIM Result as P = a5_graphQL_Execute as P (C serviceName, C queryString [, C headers [,C variables [, C queryParameters]]])

Arguments

serviceNameCharacter

The name of the GraphQL service. E.g. northwind.a5svc (the .a5svc extension is optional. If the service was defined in a different Web Project you can qualify the service name with the project name (e.g. [demoProject]\northwind.a5svc).

queryStringCharacter

The GraphQL query definition.

headersCharacter

Default = "". If the service requires authentication and if the api key is passed in the header, specify the headers.

variablesCharacter

Default = "". If the queryString uses variables, the value for each variable (JSON format).

queryParametersCharacter

Default = "". If the service requires authentication and if the api key is passed in the query string, specify the query string.

Returns

ResultPointer

Returns an object with the following properties:

errorLogical

A .t. or .f. value indicating whether or not an error occurred.

errorTextCharacter

If error is .t., contains additional information about the error.

responseCodeNumeric

The HTTP Response Code. If 200, call was a success.

resultCharacter

The query result in JSON format.

Description

Executes a GraphQL query.

Discussion

The a5_graphQL_execute() function executes a GraphQL query against a GraphQL service defined in Alpha Anywhere.

This function is not intended to execute against a 3rd party GraphQL service. To execute a query against a 3rd party GraphQL service, use the Xbasic CURL object to make an HTTP POST request.

Assume you have built a service called Northwind.a5svc in the current Web Project. Here is a script to execute a query against this service:

dim serviceName as c = "[demo]\northwind"
dim queryString as c = <<%str%
{
    cust1: GetCustomer(CustomerID: $var1) {
        CustomerID
        ContactName
        CompanyName
        City
        Country
    }
    cust2: GetCustomer(CustomerID: $var2) {
        CustomerID
        ContactName
        CompanyName
        City
        Country
        ContactTitle
        Orders {
            OrderID
            OrderDate
            OrderDetails {
                ProductID
                Quantity
                UnitPrice
                __typename
            }
        }
    }
}
%str%

dim variables as c = <<%str%
{
    "var1": "blaus",
    "var2": "bolid"
}
%str%

dim result as p 
result = a5_graphQL_Execute(serviceName, queryString, "", variables)

if result.error = .f. then 
    DIM jsonResult as c = result.result
    jsonResult = convert_utf8_to_acp(jsonResult)
    showvar(jsonResult)
else
    showvar(result.errortext)
end if

Here is the result returned by this query:

{
    "data": {
        "cust1": {
            "CustomerID": "BLAUS",
            "ContactName": "Hanna Moos",
            "CompanyName": "Blauer See Delikatessen",
            "City": "Mannheim",
            "Country": "Germany"
        },
        "cust2": {
            "CustomerID": "BOLID",
            "ContactName": "Martín Sommer",
            "CompanyName": "Bólido Comidas preparadas",
            "City": "Madrid",
            "Country": "Spain",
            "ContactTitle": "Owner",
            "Orders": [
                {
                    "OrderID": 10326,
                    "OrderDate": "1996-10-10 00:00:00",
                    "OrderDetails": [
                        {
                            "ProductID": 4,
                            "Quantity": 24,
                            "UnitPrice": 17.6,
                            "__typename": "OrderDetail"
                        },
                        {
                            "ProductID": 57,
                            "Quantity": 16,
                            "UnitPrice": 15.6,
                            "__typename": "OrderDetail"
                        },
                        {
                            "ProductID": 75,
                            "Quantity": 50,
                            "UnitPrice": 6.2,
                            "__typename": "OrderDetail"
                        }
                    ]
                },
                {
                    "OrderID": 10801,
                    "OrderDate": "1997-12-29 00:00:00",
                    "OrderDetails": [
                        {
                            "ProductID": 17,
                            "Quantity": 40,
                            "UnitPrice": 39,
                            "__typename": "OrderDetail"
                        },
                        {
                            "ProductID": 29,
                            "Quantity": 20,
                            "UnitPrice": 123.79,
                            "__typename": "OrderDetail"
                        }
                    ]
                },
                {
                    "OrderID": 10970,
                    "OrderDate": "1998-03-24 00:00:00",
                    "OrderDetails": [
                        {
                            "ProductID": 52,
                            "Quantity": 40,
                            "UnitPrice": 7,
                            "__typename": "OrderDetail"
                        }
                    ]
                }
            ]
        }
    }
}

See Also